/*  ---- TASK 2 -----
 *  Learning goals:
 *    - Reading the datasheet.
 *    - Reading from a port (reading from a register)
 *    - Writing to a port (writing to a register)
 *    - Conditional C statements and program flow logic
 *    - Functions
 */

#include <avr/io.h>

#define LED_PORT PORTB
#define LED_DDR  DDRB

#define SWITCH_PORT PORTB
#define SWITCH_DDR  DDRB
#define SWITCH_PIN  PINB

#define SW0  0
#define SW1  1
#define LED2 2
#define LED3 3

#define ON  1
#define OFF 0


void led_on (uint8_t led);       // Turn on the led
void led_off(uint8_t led);       // Turn off the led

char read_switch (uint8_t sw);   // return value of sw


int main( void ) {
	LED_DDR |= (1 << LED2)|(1 << LED3);	//	Set led port 2 and 3 as output
	SWITCH_DDR &= ~((1 << SW0)|(1 << SW1));	//	Set switch port 0 and 1 as input (by def 0 so not necessary)
	SWITCH_PORT |= (1 << SW0)|(1 << SW1);	//	Enable pull-up on switch port 0 and 1
	
	while (1) {
		if (read_switch(SW0)){
			led_on(LED2);
		} else {
			led_off(LED2);
		}
		
		if (read_switch(SW1)){
			led_on(LED3);
		} else {
			led_off(LED3);
		}
		
	}
}



void led_on (uint8_t led){
	// write the LED_PORT register to set the led
	// How can you do this without affecting the other leds?
	// remember that the led is on when the output is 0/GND!
	LED_PORT &= ~(1 << led);
}
void led_off(uint8_t led){
	// write the LED_PORT register to clear the led
	LED_PORT |= (1 << led);
}


char read_switch (uint8_t sw){
	// read the state of the switch and return it.	
	// how can you read the state of only one switch without being disturbed by the others?
	// remember that when the switch is pressed, the input is 0

	if (!(SWITCH_PIN & (1 << sw))){
		return ON;
	} else {
		return OFF;
	}

}



